<template>
  <div>
    <page-tool>
      <el-button slot="right" type="primary" size="small" @click="addPermission(1,'0')">添加</el-button>
    </page-tool>
    <el-table
      v-loading="loading"
      :data="allPermission"
      row-key="id"
      border
      style="width: 100%"
    >
      <el-table-column
        prop="name"
        align="center"
        label="名称"
      />
      <el-table-column
        prop="code"
        label="标识"
        align="center"
      />
      <el-table-column
        prop="description"
        align="center"
        label="描述"
      />
      <el-table-column
        label="操作"
        align="center"
      >
        <template slot-scope="{row}">
          <el-button v-if="row.type === 1" type="text" @click="addPermission(2,row.id)">添加</el-button>
          <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
          <el-button type="text" @click="del(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="showDialog" :title="title" width="40%" @close="btnCancel">
      <el-form ref="form" label-width="80px" :model="formData" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width: 80%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width: 80%" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" style="width: 80%" />
        </el-form-item>
        <el-form-item label="企业可见">
          <el-switch
            v-model="formData.enVisible"
            active-text="不可见"
            inactive-text="可见"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button size="small" type="primary" :loading="OKLoading" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getAllPermission, delPermission, addPermission, getPermissionById, updatePermission } from '@/api/permission'
import { dataToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      allPermission: [],
      loading: false,
      showDialog: false,
      title: '',
      OKLoading: false,
      formData: {
        name: '',
        code: '',
        description: '',
        enVisible: ''
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getAllPermission()
  },
  methods: {
    // 获取所有权限
    async getAllPermission() {
      this.loading = true
      this.allPermission = dataToTreeData(await getAllPermission(), '0')
      this.loading = false
    },
    // 删除权限点
    async del(id) {
      try {
        await delPermission(id)
        this.$message.success('删除成功')
        // 重新渲染页面，获取最新的权限点数据
        await this.getAllPermission()
      } catch (error) {
        this.$message.warning(error)
      }
    },
    // 根据不同地方的添加添加不一样的type和pid
    addPermission(type, id) {
      this.title = '添加权限'
      this.formData.type = type
      this.formData.pid = id
      this.showDialog = true
    },
    // 编辑权限点
    async editPermission(id) {
      this.title = '编辑权限'
      this.formData = await getPermissionById(id)
      this.showDialog = true
    },
    // 添加权限
    async btnOK() {
      try {
        await this.$refs.form.validate()
        this.OKLoading = true
        // 根据是否存在有id来判断是处于编辑状态还是添加状态
        if (this.formData.id) {
          // 编辑
          await updatePermission(this.formData)
        } else {
          await addPermission(this.formData)
        }
        const message = this.formData.id ? '修改权限点成功' : '添加权限点成成功'
        this.$message.success(message)
        // 重新拉去数据，关闭会话框
        // this.OKLoading = false
        this.getAllPermission()
        // this.showDialog = false
      } catch (error) {
        this.$message.warning(error)
      } finally {
        this.OKLoading = false
        this.showDialog = false
      }
    },
    // 点击取消
    btnCancel() {
      this.formData = {
        name: '',
        code: '',
        description: '',
        enVisible: ''
      }
      this.showDialog = false
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style>

</style>
